// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/collectrule_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/collectrule"
	collectruleModel "gin-sns/app/model/module/collectrule"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var CollectruleService = newCollectruleService()

func newCollectruleService() *collectruleService {
	return &collectruleService{}
}

type collectruleService struct {
}

//根据主键查询数据
func (s *collectruleService) SelectRecordById(id int64) (*collectruleModel.Entity, error) {
	return collectrule.CollectruleDao.SelectById(id)
}

//
func (s *collectruleService) SelectRecordsIn(idarr []int64) ([]collectruleModel.Entity, error) {

	list, err := collectrule.CollectruleDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *collectruleService) DeleteRecordById(id int64) bool {
	return collectrule.CollectruleDao.DeleteById(id)
}

//批量删除数据记录
func (s *collectruleService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := collectrule.CollectruleDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *collectruleService) AddSave(req *collectruleModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity collectruleModel.Entity

	//entity.Id = req.Id
	entity.Title = req.Title
	entity.Rule = req.Rule
	entity.Description = req.Description
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := collectrule.CollectruleDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *collectruleService) EditSave(req *collectruleModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &collectruleModel.Entity{Id: req.Id}
	ok, err := collectrule.CollectruleDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.Title = req.Title
	entity.Rule = req.Rule
	entity.Description = req.Description
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return collectrule.CollectruleDao.Update(entity)
}

//根据条件查询数据
func (s *collectruleService) SelectListAll(params *collectruleModel.SelectPageReq) ([]collectruleModel.Entity, error) {
	return collectrule.CollectruleDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *collectruleService) SelectListByPage(params *collectruleModel.SelectPageReq) ([]collectruleModel.Entity, *page.Paging, error) {
	return collectrule.CollectruleDao.SelectListByPage(params)
}

// 导出excel
func (s *collectruleService) Export(param *collectruleModel.SelectPageReq) (string, error) {
	head := []string{"", "名称", "规则", "描述", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "title", "rule", "description", "status", "create_time"}
	return collectrule.CollectruleDao.SelectListExport(param, head, col)
}
